package com.shycloud.mido.member.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.BasicComboGoods;
import com.shycloud.mido.member.api.entity.BasicGoodsInfo;
import com.shycloud.mido.member.api.entity.PersonalCombVO;
import com.shycloud.mido.member.api.vo.BasicGoodsInfoVO;
import com.shycloud.mido.member.api.vo.GoodsInfoVO;
import com.shycloud.mido.member.api.vo.ProductsComboVO;
import com.shycloud.mido.member.api.vo.MallCreateOrderVO;
import com.shycloud.mido.member.service.ShoppingMallService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

/**
 * @author Xiaoxing Yu
 * @date 2020/8/31
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/shopping")
@ApiIgnore
public class ShoppingMallController {

	private ShoppingMallService shoppingMallService;

	/**
	 * 获取所有可编辑价格商品列表
	 *
	 * @return
	 */
	@GetMapping("getEditableGoods")
	@ApiOperation("获取所有可编辑价格商品列表")
	public R getEditableGoods() {
		return new R(shoppingMallService.getEditableGoods());
	}

	/**
	 * 获取虚拟教学商品编辑列表
	 *
	 * @return
	 */
	@GetMapping("getVirtualGoods")
	@ApiOperation("获取虚拟教学商品编辑列表")
	public R getVirtualGoods() {
		return new R(shoppingMallService.getVirtualGoods());
	}

	/**
	 * 编辑一件虚拟教学商品
	 *
	 * @return
	 */
	@PostMapping("editVirtualGoods")
	@ApiOperation("编辑一件虚拟教学商品")
	public R editVirtualGoods(@RequestBody Map map) {
		return new R(shoppingMallService.editVirtualGoods(map));
	}

	/**
	 * 编辑一件商品价格等信息
	 *
	 * @return orderId 返回订单号
	 */
	@PostMapping("editGoods")
	@ApiOperation("编辑一件商品价格等信息")
	public R editGoods(@RequestBody GoodsInfoVO goodsInfoVO) {
		return new R(shoppingMallService.editGoods(goodsInfoVO));
	}

	/**
	 * 根据类别查询商品列表
	 *
	 * @return
	 */
	@GetMapping("getGoods")
	@ApiOperation("根据类别查询商品列表")
	public R getGoods(Integer categoryId, String goodsName) {
		return new R(shoppingMallService.getGoods(categoryId, goodsName));
	}

	/**
	 * 新增商品
	 *
	 * @param goodsInfoVO
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/9/24 10:29
	 **/
	@PostMapping("addGoods")
	public R addGoods(@RequestBody BasicGoodsInfoVO goodsInfoVO) {
		return new R(shoppingMallService.addGoods(goodsInfoVO));
	}

	/**
	 * 获取所有套餐列表
	 *
	 * @return
	 */
	@GetMapping("getAllComboList")
	@ApiOperation("获取所有套餐列表")
	public R getAllComboList() {
		List<ProductsComboVO> comboList = shoppingMallService.getAllComboList(false);
		return new R<List<ProductsComboVO>>(CommonConstants.SUCCESS, "获取列表成功", comboList);
	}

	/**
	 * 获取所有上架套餐列表
	 *
	 * @return
	 */
	@GetMapping("getActiveComboList")
	@ApiOperation("获取所有上架套餐列表")
	public R getAllActiveComboList() {
		List<ProductsComboVO> comboList = shoppingMallService.getAllComboList(true);
		return new R<List<ProductsComboVO>>(CommonConstants.SUCCESS, "获取列表成功", comboList);
	}

	/**
	 * 增加新套餐
	 *
	 * @param comboVO
	 * @return
	 */
	@PostMapping("addNewCombo")
	@ApiOperation("增加新套餐")
	public R addNewCombo(@RequestBody ProductsComboVO comboVO) {
		Boolean res = shoppingMallService.addNewCombo(comboVO);
		return res == true ? new R<>(CommonConstants.SUCCESS, "成功添加套餐", true)
				: new R<>(CommonConstants.FAIL, "添加套餐失败", false);
	}

	/**
	 * 增加新套餐
	 *
	 * @param comboVO
	 * @return
	 */
	@PostMapping("editCombo")
	@ApiOperation("编辑套餐")
	public R editCombo(@RequestBody ProductsComboVO comboVO) {
		Boolean res = shoppingMallService.editCombo(comboVO);
		return res == true ? new R<>(CommonConstants.SUCCESS, "成功编辑套餐", true)
				: new R<>(CommonConstants.FAIL, "编辑套餐失败", false);
	}

	/**
	 * 启用/禁用套餐
	 *
	 * @param comboVO
	 * @return
	 */
	@PostMapping("triggerCombo")
	@ApiOperation("启用/禁用套餐")
	public R triggerCombo(@RequestBody ProductsComboVO comboVO) {
		Boolean res = shoppingMallService.triggerCombo(comboVO);
		return res == true ? new R<>(CommonConstants.SUCCESS, "套餐状态更改成功", true)
				: new R<>(CommonConstants.FAIL, "套餐状态更改失败", false);
	}

	/**
	 * 删除套餐
	 *
	 * @param paramsJson
	 * @return
	 */
	@PostMapping("deleteCombo")
	@ApiOperation("删除套餐")
	public R deleteCombo(@RequestBody String paramsJson) {
		JSONObject paramsJSONObject = JSONObject.parseObject(paramsJson);
		Integer comboId = paramsJSONObject.getInteger("comboId");
		Boolean res = shoppingMallService.deleteCombo(comboId);
		return res == true ? new R<>(CommonConstants.SUCCESS, "套餐删除成功", true)
				: new R<>(CommonConstants.FAIL, "套餐删除失败", false);
	}

	/**
	 * 增加新个人套餐(根据optId去存)
	 *
	 * @param comboVO
	 * @return
	 */
	@PostMapping("addPersonalCombo")
	@ApiOperation("增加新个人套餐")
	public R addPersonalCombo(@RequestBody PersonalCombVO comboVO) {
		Boolean res = shoppingMallService.addPersonalCombo(comboVO);
		return res == true ? new R<>(CommonConstants.SUCCESS, "成功添加套餐", true)
				: new R<>(CommonConstants.FAIL, "添加套餐失败", false);
	}

	/**
	 * 获取所有自定义个人套餐(根据optId去获取)
	 *
	 * @param optId
	 * @return
	 */
	@GetMapping("getPersonalCombo")
	@ApiOperation("获取所有自定义个人套餐")
	public R getPersonalCombo(Integer optId) {
		return new R(shoppingMallService.getPersonalCombo(optId));
	}

	/**
	 * 创建后台购买订单
	 * 虚拟套餐/自定义套餐/单买
	 *
	 * @return orderId 返回订单号
	 */
	@PostMapping("createOrder")
	@ApiOperation("创建后台商城购买订单")
	public R createOrder(@RequestBody MallCreateOrderVO orderVO, HttpServletRequest request) {
		return new R(shoppingMallService.createOrder(orderVO, request));
	}

	/**
	 * 查询订单支付状态
	 * @return
	 */
	@GetMapping("queryOrderPayment")
	@ApiOperation("查询订单支付状态")
	public R queryOrderPayment(String paymentMethod, String orderNo) {
		return new R(shoppingMallService.queryOrderPayment(paymentMethod, orderNo));
	}

	/**
	 * 删除后台购买订单
	 *
	 * @return orderId 返回订单号
	 */
	@PostMapping("deleteOrder")
	@ApiOperation("创建后台商城购买订单")
	public R deleteOrder(@RequestBody String paramsJson) {
		JSONObject paramsJSONObject = JSONObject.parseObject(paramsJson);
		Long orderNo = paramsJSONObject.getLong("orderNo");
		return new R(shoppingMallService.deleteOrder(orderNo));
	}

	/**
	 * 分页查询后台购买订单
	 *
	 * @return
	 */
	@GetMapping("pageOrders")
	@ApiOperation("分页查询后台订单")
	public R pageOrders(Integer current, Integer size,
						String startDate, String endDate, String paymentMethod, String orderNo,
						String status, Integer optId, Integer orgId) {
		return new R(
				shoppingMallService.pageOrders(
						new Page(current, size), startDate, endDate, paymentMethod, orderNo, status, optId, orgId));
	}

	/**
	 * 计算在线课价格接口
	 *
	 * @return
	 */
	@GetMapping("calOnlineCoursePrice")
	@ApiOperation("计算在线课价格接口")
	public R calOnlineCoursePrice(Integer duration) {
		return new R(shoppingMallService.calOnlineCoursePrice(duration));
	}

	/**
	 * 计算纠错大师租赁权限价格接口
	 *
	 * @return
	 */
	@GetMapping("calMasterPrice")
	@ApiOperation("计算在线课价格接口")
	public R calMasterPrice(Integer duration) {
		return new R(shoppingMallService.calMasterPrice(duration));
	}

	/**
	 * 获取所有纠错大师商品信息
	 *
	 * @return
	 */
	@GetMapping("getMasterGoods")
	@ApiOperation("获取所有纠错大师商品信息")
	public R getAllMasterGoods() {
		return new R(shoppingMallService.getMasterGoods());
	}

	/**
	 * 获取商品分类
	 *
	 * @return
	 */
	@GetMapping("getGoodsCategory")
	@ApiOperation("获取商品分类")
	public R getGoodsCategory() {
		return new R(shoppingMallService.getGoodsCategory());
	}

	/**
	 * 根据GoodsId获取价格
	 * @return
	 */
	@GetMapping("getPriceByGoodsId")
	@ApiOperation("根据GoodsId获取价格")
	public R getPriceByGoodsId(Integer goodsId) {
		return new R(shoppingMallService.getPriceByGoodsId(goodsId));
	}


	/**
	 * 根据ComboId获取商品
	 * @return
	 */
	@GetMapping("getGoodsByCombo")
	@ApiOperation("根据GoodsId获取价格")
	public List<BasicComboGoods> getGoodsByComboId(Integer comboId) {
		return shoppingMallService.getGoodsByCombo(comboId);
	}

	/**
	 * 根据关键字获取商品信息
	 * @return
	 */
	@GetMapping("matchGoodsByKey")
	@ApiOperation("根据关键字获取商品信息")
	public List<BasicGoodsInfo> matchGoodsInfoByKeyword(String keyword) {
		return shoppingMallService.matchGoodsInfoByKeyword(keyword);
	}

}
